// service/ReservationService.ts
import { ReservationDAO } from "../../DAO";
import { FlightService,BusService,HotelService,CustomerService } from "../../service"
import type { ReservationData } from "../../types/DataTypes";

export class ReservationService {
    // ==================== 预约操作 ====================

    // 新增预约
    static async addReservation(
        customerName: string,
        reservationType: number,
        reservationMessage: string
    ): Promise<boolean> {
        try {
            // 检测用户存在
            let isCustomerExist = await CustomerService.customerNameExists(customerName);
            if (!isCustomerExist) {
                throw new Error(`不存在的用户:${customerName}`);
            }

            // 检查对应服务的可用性
            let isAvailable = false;
            
            switch (reservationType) {
                case 1: // 航班
                    isAvailable = await FlightService.checkFlightAvailability(reservationMessage);
                    break;
                case 2: // 巴士
                    isAvailable = await BusService.checkBusAvailability(reservationMessage);
                    break;
                case 3: // 酒店
                    isAvailable = await HotelService.checkHotelAvailability(reservationMessage);
                    break;
                default:
                    throw new Error(`不支持的预约类型: ${reservationType}`);
            }

            if (!isAvailable) {
                throw new Error(`预约资源不可用: ${reservationMessage}`);
            }

            // 生成reservationKey
            const reservationKey = `${reservationType}-${reservationMessage}-${customerName}`;

            // 创建预约数据
            const reservationData: ReservationData = {
                customerName,
                reservationType,
                reservationKey
            };

            // 添加到数据库
            const result = await ReservationDAO.addReservation(reservationData);

            if (result) {
                // 调用对应服务的可用数量递减方法
                switch (reservationType) {
                    case 1:
                        await FlightService.decrementFlightAvailability(reservationMessage);
                        break;
                    case 2:
                        await BusService.decrementBusAvailability(reservationMessage);
                        break;
                    case 3:
                        await HotelService.decrementHotelAvailability(reservationMessage);
                        break;
                }
            }

            return result;
        } catch (error) {
            throw error;
        }
    }

    // 取消预约
    static async cancelReservation(
        reservationKey: string
    ): Promise<boolean> {
        try {
            // 从reservationKey中解析出reservationMessage
            const parts = reservationKey.split('-');
            if (parts.length < 3) {
                throw new Error(`无效的reservationKey格式: ${reservationKey}`);
            }

            // 验证reservationKey的格式和内容
            const keyType = parseInt(parts[0]);
            const reservationMessage = parts[1];

            // 调用对应服务的可用数量递增方法
            switch (keyType) {
                case 1:
                    await FlightService.incrementFlightAvailability(reservationMessage);
                    break;
                case 2:
                    await BusService.incrementBusAvailability(reservationMessage);
                    break;
                case 3:
                    await HotelService.incrementHotelAvailability(reservationMessage);
                    break;
                default:
                    throw new Error(`不支持的预约类型: ${reservationKey}`);
            }

            // 删除预约记录
            return await ReservationDAO.deleteReservation(reservationKey);
        } catch (error) {
            console.error('取消预约失败:', error);
            throw error;
        }
    }

    // 获取所有预约
    static async getAllReservations(): Promise<ReservationData[]> {
        return ReservationDAO.getAllReservations();
    }

    // 根据客户名查询预约
   /* static async getReservationsByCustomer(customerName: string): Promise<ReservationData[]> {
        return ReservationDAO.getReservationsByCustomer(customerName);
    }*/

    // 根据预约类型查询预约
  /*  static async getReservationsByType(reservationTypes: number | number[]): Promise<ReservationData[]> {
        return ReservationDAO.getReservationsByType(reservationTypes);
    }*/

    // 综合查询预约
    static async searchReservations(criteria: {
        customerName?: string;
        reservationTypes?: number[];
    }): Promise<ReservationData[]> {
        return ReservationDAO.searchReservations(criteria);
    }

    // 检查预约是否存在
    static async reservationExists(reservationKey: string): Promise<boolean> {
        return ReservationDAO.reservationExists(reservationKey);
    }
}